<template>
  <el-dialog
    :visible="showVisible"
    center
    :show-close="false"
    :append-to-body="true"
    width="600px">
    <div class="b g3 f16 tl" slot="title">
      <div class="ks-row-between pl15 pr15">
        <span>{{ isNew ? "新建参数" : "编辑参数" }}</span>
        <span class="el-icon-close g9 f26 poi" @click="handleCancel"></span>
      </div>
    </div>

    <el-form class="pl30 pr30" :model="formData" ref="form" label-width="120px">
      <el-form-item
        label="参数"
        prop="param"
        :rules="[
          { required: true, message: '请输入参数', trigger: 'change' },
        ]"
      >
        <el-input
          v-model="formData.param"
          style="width: 400px"
          placeholder="请输入参数"
          :maxlength="28"
          show-word-limit
        />
      </el-form-item>
    </el-form>

    <div slot="footer" class="tr">
      <el-button @click="handleCancel">取 消</el-button>
      <el-button type="primary" @click="submit">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
export default {
  name: 'AddParam',
  data() {
    return {
      showVisible: false,
      index: '', // 参数的索引
      formData: {},
      isNew: true
    }
  },

  computed: {
  },

  created() {},

  methods: {
    addForm(record = {}, index) {
      this.showVisible = true
      this.isNew = true
      this.index = index
      this.formData = {
        ...record
      }
      this.$nextTick(() => {
        this.$refs.form.clearValidate()
      })
    },
    editForm(record = {}, index) {
      this.showVisible = true
      this.isNew = false
      this.index = index
      this.formData = {
        ...record
      }
      this.$nextTick(() => {
        this.$refs.form.clearValidate()
      })
    },

    async submit() {
      try {
        await this.$refs.form.validate()
        const { param } = this.formData
        let params = {}
        params = {
          param,
          index: this.index
        }
        for (const key in params) {
          params[key] =
            typeof params[key] === 'string' ? params[key].trim() : params[key]
        }
        this.handleCancel()
        if (this.isNew) {
          this.$emit('addParam', params)
        } else {
          this.$emit('editParam', params)
        }
      } catch (error) {
        console.log(error)
      }
    },
    handleCancel() {
      this.showVisible = false
    }
  }
}
</script>

<style lang="scss" scoped>
</style>
